package dfbz.com.controller.article;

import dfbz.com.controller.BaseServlet;
import dfbz.com.model.Article;
import dfbz.com.model.Favorite;
import dfbz.com.service.AritcleService;
import dfbz.com.service.impl.ArticleServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet {

    AritcleService articleServiceImp = new ArticleServiceImp();

    public void articleList(HttpServletRequest req, HttpServletResponse resp) {

        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String articleName = req.getParameter("articleName");
        String current = req.getParameter("current");

        if (current == null) {
            current = "1";
        }
        if (!(null == articleName||"".equals(articleName))) {
            try {
                String encode = URLEncoder.encode(articleName,"UTF-8");
                resp.sendRedirect(req.getContextPath() + "/view/article?current=" + current + "&articleName=" + encode);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                resp.sendRedirect(req.getContextPath() + "/view/article?current=" + current);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    public void articleDetail(HttpServletRequest req, HttpServletResponse resp) {

        String title = req.getParameter("title");

        try {
            resp.sendRedirect(req.getContextPath()+"/view/articleDetail?title="+title);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void collectArticle(HttpServletRequest req,HttpServletResponse resp){

        String cId = req.getParameter("cId");
        Object userId = req.getSession().getAttribute("userId");
//        System.out.println("进来这里了-------------------");
//        System.out.println(cId);
//        这里去添加收藏，再跳到这本书细节
        Favorite favorite = new Favorite();
        favorite.setAid(Integer.parseInt(cId));
        favorite.setUid(Integer.parseInt(userId.toString()));
        favorite.setId(null);
        articleServiceImp.collectArticle(favorite);

        try {
            resp.sendRedirect("/view/articleDetail?bookId="+cId);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public void cancelCollect(HttpServletRequest req,HttpServletResponse resp){
        String cId = req.getParameter("cId");
        Object userId = req.getSession().getAttribute("userId");
//        System.out.println("进来这里-------------");
//        System.out.println(cId);
//        这里去删除收藏，再跳到文章细节

        articleServiceImp.cancelCollect(Integer.parseInt(cId),Integer.parseInt(userId.toString()));

        try {
            resp.sendRedirect("/view/articleDetail?bookId="+cId);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public void myCollection (HttpServletRequest req,HttpServletResponse resp){

        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String articleName = req.getParameter("articleName");
        String current = req.getParameter("current");

        if (current == null) {
            current = "1";
        }

        if (!(null == articleName||"".equals(articleName))) {
            try {
                String encode = URLEncoder.encode(articleName,"UTF-8");
                resp.sendRedirect(req.getContextPath() + "/view/articleCollect?current=" + current + "&articleName=" + encode);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                resp.sendRedirect(req.getContextPath() + "/view/articleCollect?current=" + current);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    public void publishArticle(HttpServletRequest req,HttpServletResponse resp){


        try {
            resp.sendRedirect(req.getContextPath()+"/view/articleAdd");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void addArticle(HttpServletRequest req,HttpServletResponse resp){

        try {
            req.setCharacterEncoding("utf-8");
            String title = req.getParameter("title");
            String content = req.getParameter("content");

//            System.out.println(myDetail);
//            System.out.println(title);
//            System.out.println(content);
            if (!("".equals(title)||"".equals(content))){
                Map myDetail = (Map)req.getSession().getAttribute("myDetail");

//                System.out.println("现在才是去存入数据库");
                Article article = new Article();
                article.setBrowseCount(0);
                article.setContent(content);
                article.setId(null);
                article.setPublishDate(new Date());
                article.setTitle(title);
                article.setUserId((Integer) myDetail.get("user_id"));
                article.setPublishUsername((String) myDetail.get("username"));

                articleServiceImp.addArticle(article);
            }


            resp.sendRedirect(req.getContextPath()+"/view/article?current=1");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}
